import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const commonRoutes = [
    {
        path: '/register',
        name: 'register',
        meta: { title: '注册' },
        component: () => import('../components/register.vue'),
    },
    {
        path: '/login',
        name: 'login',
        meta: { title: '登录' },
        component: () => import('../components/Login.vue'),
    },
    {
        path: '/404',
        name: '404',
        meta: { title: '404' },
        component: () => import('../components/404.vue'),
    },
    {
        path: 'password',
        name: 'password',
        meta: { title: '修改密码' },
        component: () => import('../views/Password.vue'),
    },
    { path: '/', redirect: '/home' },
]

// 本地所有的页面 需要配合后台返回的数据生成页面
export const asyncRoutes = {
    home: {
        path: 'home',
        name: 'home',
        meta: { title: '主页' },
        component: () => import('../views/Home.vue'),
    },
    library: {
        path: 'library',
        name: 'library',
        meta: { title: '图书馆' },
        component: () => import('../views/library.vue'),
    },
    bookdetail: {
        path: 'bookdetail',
        name: 'bookdetail',
        meta: { title: '书籍信息' },
        component: () => import('../views/bookdetail.vue'),
    },
    password: {
        path: 'password',
        name: 'password',
        meta: { title: '修改密码 ' },
        component: () => import('../views/Password.vue'),
    },
    message: {
        path: 'message',
        name: 'message',
        meta: { title: '消息通知' },
        component: () => import('../views/message.vue'),
    },
    myinfo: {
        path: 'myinfo',
        name: 'myinfo',
        meta: { title: '个人信息' },
        component: () => import('../views/myinfo.vue'),
    },
    mybook: {
        path: 'mybook',
        name: 'mybook',
        meta: { title: '我的二手书' },
        component: () => import('../views/mybook.vue'),
    },
    addbook: {
        path: 'addbook',
        name: 'addbook',
        meta: { title: '增加二手书' },
        component: () => import('../views/addbook.vue'),
    },
    order: {
        path: 'order',
        name: 'order',
        meta: { title: '我的订单' },
        component: () => import('../views/order.vue'),
    },
}

const createRouter = () => new Router({
    routes: commonRoutes,
})

const router = createRouter()

export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher
}

export default router
